<?php

class ContactusController extends GTL_Action {

    public function init() {
        parent::init();
        /* Initialize action controller here */
    }

    
    public function indexAction() {
         $errorMessage = array();
         $successmessage = "";
		 $postedData = array();
        if ($this->getRequest()->isPost()) {
            
              $postedData = $this->_request->getPost();
             
              if($postedData['name'] == 'Name:'){
                  $errorMessage[] = "Please enter the name";
              }if($postedData['email'] == 'Email:'){
                  $errorMessage[] = "Please enter the email";
              }
              if(!filter_var($postedData['email'], FILTER_VALIDATE_EMAIL)) {
                $errorMessage[] = "Invalid email address"; 
              }
              if($postedData['mobile'] == 'Mobile no:'){
                  $errorMessage[] = "Please enter the mobile number";
              }
              if($postedData['message'] == ''){
                  $errorMessage[] = "Please enter the message";
              }
              if (count($errorMessage) > 0) {
              } else {
                

			$contactModel = new Model_Contact();
			$contactArray = Array();
			
			$contactArray['ctc_id'] = null;
			$contactArray['ctc_name'] = $postedData['name'];
			$contactArray['ctc_email'] = $postedData['email'];
			$contactArray['ctc_mobile'] = $postedData['mobile'];
			$contactArray['ctc_message'] = $postedData['message'];
			$contactArray['ctc_createddate'] = date('Y-m-d H:i:s');
			$contactModel->save($contactArray, 'ctc_id');
				
              $body = "<html>
				<head>
				</head>
				<body>
				    <p>Hello Admin</p>
				    <p>You have received a new message from ".$postedData['name']." </p>
				    <p>E-mail id: ".$postedData['email']."</p>
					<p>Contact No.: ".$postedData['mobile']."</p>
					<p>Message: ".$postedData['message']."</p>
				</body>
				</html>
				";

				
				
				
				$metaDataModel = new Model_Metadata();
				$ADMIN_MAIL_DATA = $metaDataModel->fetchEntryByKey("ADMIN_FROM_EMAIL");
				$ADMIN_MAIL = $ADMIN_MAIL_DATA['mtd_value'];
				
				$ADMIN_FROM_DATA = $metaDataModel->fetchEntryByKey("ADMIN_FROM_NAME");
				$ADMIN_FROM = $ADMIN_FROM_DATA['mtd_value'];
				
				$mail = new Zend_Mail();
				$mail->setBodyText($body);
				$mail->setBodyHtml($body, "iso-8859-1");
				$mail->addTo($ADMIN_MAIL, $ADMIN_FROM);
				$mail->setReplyTo($ADMIN_MAIL, $ADMIN_FROM);
				$mail->addTo($ADMIN_MAIL, $ADMIN_FROM);
				$mailSubject = "Contact Us";
				$mail->addHeader('MIME-Version', '1.0');
				$mail->addHeader('Content-Transfer-Encoding', '8bit');
				$mail->addHeader('X-Mailer:', 'PHP/' . phpversion());
				$mail->setSubject($mailSubject);

				try {
					$mail->send();
                    $this->_helper->redirector('thankyou', 'contactus', 'default');
				} catch (Zend_Exception $e) {
					$errorMessage[] = "Due to technical problem, email has not been sent. Please try later or contact webmaster.";
				}
                }
        }
        $this->view->postedData = $postedData;
        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
    }

    public function thankyouAction(){
        
    } 
    
}

?>
